package com.liao.comtentresolver;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_arg;
    private Button btn_insert;
    private Button btn_delete;
    private Button btn_update;
    private Button btn_query;
    private ContentResolver resolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        et_arg = (EditText) findViewById(R.id.et_arg);
        btn_insert = (Button) findViewById(R.id.btn_insert);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_query = (Button) findViewById(R.id.btn_query);

        btn_insert.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_query.setOnClickListener(this);

        resolver = getContentResolver();
    }

    private static final String TAG = "MainActivity";

    @Override
    public void onClick(View v) {
        Uri uri;
        String text = et_arg.getText().toString().trim();
        switch (v.getId()) {
            case R.id.btn_insert:
                Uri insert;
                uri = Uri.parse("content://com.liao.myapplication/insert_" + text);
                ContentValues values = new ContentValues();
                if ("book".equals(text)) {
                    values.put("bookname", "书名");
                    values.put("author", "作者");
                } else if ("user".equals(text)) {
                    values.put("username", "小敏");
                    values.put("age", 18);
                    values.put("phone", "15255555555");
                }
                insert = resolver.insert(uri, values);
                Log.e(TAG, "onClick: insert=" + insert);
                break;
            case R.id.btn_delete:
                uri = Uri.parse("content://com.liao.myapplication/delete_" + text);
                int delete = resolver.delete(uri, "id>?", new String[]{"5"});
                Log.e(TAG, "onClick: delete=" + delete);
                break;
            case R.id.btn_update:
                uri = Uri.parse("content://com.liao.myapplication/update_" + text);
                ContentValues contentValues = new ContentValues();
                if ("book".equals(text)) {
                    contentValues.put("bookname", "书名(改)");
                    contentValues.put("author", "作者(改)");
                } else if ("user".equals(text)) {
                    contentValues.put("username", "小明(改)");
                    contentValues.put("age", "22(改)");
                    contentValues.put("phone", "18563254584(改)");
                }
                int update = resolver.update(uri, contentValues, "id>5", null);
                Log.e(TAG, "onClick: update=" + update);
                break;
            case R.id.btn_query:
                uri = Uri.parse("content://com.liao.myapplication/select_" + text);
                Cursor query = resolver.query(uri, null, null, null, null, null);
                if (query != null) {
                    while (query.moveToNext()) {
                        int id = query.getInt(0);
                        String s1 = query.getString(1);
                        String s2 = query.getString(2);
                        if ("user".equals(text)) {
                            String s3 = query.getString(3);
                            Log.e("TAG", "onClick: query=" + id + "====" + s1 + "=====" + s2 + "=======" + s3);
                        } else {
                            Log.e("TAG", "onClick: query=" + id + "====" + s1 + "=====" + s2);
                        }
                    }
                }
                if (query != null) {
                    query.close();
                }
                break;
        }
    }
}
